<template>
    <div>
        <van-row style="margin-top: 20%">
            <van-col align="center" span="8" style="margin-top: 2rem">
                <van-image
                        round
                        width="5rem"
                        height="5rem"
                        fit="cover"
                        :src="userinfo.headimgurl"
                />
            </van-col>
            <van-col span="12" style="margin-top: 2rem;height:5rem;font-size: 1.5rem"><p align="left">{{userinfo.nickname}}</p></van-col>

        </van-row>

        <van-button style="margin-bottom: 2rem;margin-top: 2rem" type="primary" @click="registerWxUser()" block>授权电脑登陆</van-button>

    </div>
</template>

<script>
    import Notify from "vant/lib/notify";
    import  {getAction} from '@/api/manage'

    export default {
        name: "PCAuth",
        data() {
            return {
                cs: '测试',
                phoneNumber: '',
                userinfo: {headimgurl:'https://img.yzcdn.cn/vant/cat.jpeg',nickname: '测试'},
                openid: window.localStorage.getItem('openid'),
                authorization: '',
                ws: null,
                userId: this.$route.params.clientid,
                appName: this.$route.params.appName

            }
        },
        mounted() {
            //    Notify({message: JSON.stringify(window.localStorage.getItem('userinfo')),type:'danger',duration: 15000})
            this.userinfo = JSON.parse(window.localStorage.getItem('userinfo'));
           // this.openid = 'oQtMMwkcwDP1Ex27WxOKcu933lbM'
            getAction('/getAuthorization',this.openid).then(res=>{
                console.log(res)
                this.reg=res.success;
                if(this.reg === false){
                    Notify({message:'用户未注册:',type:'warning',duration: 1500});
                    this.$router.push({          path: '/'});
                    }
                else{
                    this.authorization = res.result.authorization;
                    this.ws = new WebSocket("ws://"+window.location.host+"/socketServer/" + this.openid);

                    this.ws.onmessage = function (evt) {
                        console.log(evt)
                        console.log(evt.data)
                        const userRelateAccount = JSON.parse(evt.data)
                        if (userRelateAccount.status === 200) {
                            Notify({message:'授权登录成功',type:'success',duration:2000})
                        } else {
                            Notify({message:'授权登录失败,该用户没有权限',type:'danger',duration:2000})
                        }
                    };
                }
            }).catch(err=>{
                this.reg=false;
                this.err =err

                Notify({message:'服务连接超时:'+err,type:'danger',duration: 1500})
            })
        },
        methods: {
            registerWxUser(){
                if (this.authorization.indexOf("PC登录") != -1) {
                    console.log(">>>>>",this.appName)
                    const socketMsg = { toUser: this.userId,type:'1',appName:this.appName};
                    this.ws.send(JSON.stringify(socketMsg))
                }else{
                    Notify({message:'该用户没有pc端登陆权限',type:'warning',duration: 2000});

                }
            }
        }
    }
</script>

<style scoped>

</style>